package com.liuguang.framework.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Build.VERSION;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import java.util.ArrayList;
import java.util.List;

public class PermissionUtil {
    public static final int CODE_MULTI_PERMISSION = 5;
    private static String[] PERMISSIONS_CALL_PHONE = {"android.permission.CALL_PHONE"};
    private static String[] PERMISSIONS_CAMERA = {"android.permission.CAMERA"};
    private static String[] PERMISSIONS_STORAGE = {"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE"};
    private static String[] PERMISSION_LOCATION = {"android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"};
    private static String[] PERMISSION_PHONE_STATE = {"android.permission.READ_PHONE_STATE"};
    private static String[] PERMISSION_RECORD_AUDIO = {"android.permission.RECORD_AUDIO"};
    public static final int REQUEST_CALL_PHONE = 7;
    public static final int REQUEST_CAMERA = 2;
    public static final int REQUEST_EXTERNAL_STORAGE = 1;
    public static final int REQUEST_LOCATION = 6;
    public static final int REQUEST_PHONE_STATE = 3;
    public static final int REQUEST_RECORD_AUDIO = 4;
    public static onRequestPermissionsResultCallbacks callBack;
    private static final boolean isKitKat = (VERSION.SDK_INT >= 23);
    private static String[] requestPermissions = {"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_PHONE_STATE", "android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"};

    public interface onRequestPermissionsResultCallbacks {
        void onPermissionsDenied(int i, List<String> list, boolean z);

        void onPermissionsGranted(int i, List<String> list, boolean z);
    }

    public static boolean verifyLocationPermissions(Activity activity) {
        if (!isKitKat) {
            return true;
        }
        try {
            int checkSelfPermission = ActivityCompat.checkSelfPermission(activity, "android.permission.ACCESS_COARSE_LOCATION");
            int checkSelfPermission2 = ActivityCompat.checkSelfPermission(activity, "android.permission.ACCESS_FINE_LOCATION");
            if (checkSelfPermission == 0 || checkSelfPermission2 == 0) {
                return true;
            }
            ActivityCompat.requestPermissions(activity, PERMISSION_LOCATION, 6);
            return false;
        } catch (Exception unused) {
            return false;
        }
    }

    public static boolean verifyStoragePermissions(Activity activity, boolean z) {
        if (!isKitKat) {
            return true;
        }
        try {
            int checkSelfPermission = ActivityCompat.checkSelfPermission(activity, "android.permission.WRITE_EXTERNAL_STORAGE");
            int checkSelfPermission2 = ActivityCompat.checkSelfPermission(activity, "android.permission.READ_EXTERNAL_STORAGE");
            if (checkSelfPermission == 0) {
                if (checkSelfPermission2 == 0) {
                    return true;
                }
            }
            if (z) {
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, 1);
            }
            return false;
        } catch (Exception unused) {
            return false;
        }
    }

    public static boolean verifyCameraPermission(Activity activity) {
        if (!isKitKat) {
            return true;
        }
        try {
            if (ActivityCompat.checkSelfPermission(activity, "android.permission.CAMERA") == 0) {
                return true;
            }
            ActivityCompat.requestPermissions(activity, PERMISSIONS_CAMERA, 2);
            return false;
        } catch (Exception unused) {
            return false;
        }
    }

    public static boolean verifyPhoneStatePermission(Context context, boolean z) {
        if (!isKitKat || ActivityCompat.checkSelfPermission(context, "android.permission.READ_PHONE_STATE") == 0) {
            return true;
        }
        if (z) {
            ActivityCompat.requestPermissions((Activity) context, PERMISSION_PHONE_STATE, 3);
        }
        return false;
    }

    public static boolean verifyCallPhonePermission(Activity activity) {
        if (!isKitKat) {
            return true;
        }
        try {
            if (ActivityCompat.checkSelfPermission(activity, "android.permission.CALL_PHONE") == 0) {
                return true;
            }
            ActivityCompat.requestPermissions(activity, PERMISSIONS_CALL_PHONE, 7);
            return false;
        } catch (Exception unused) {
            return false;
        }
    }

    public static boolean verifyRecordAudioPermission(Activity activity) {
        if (!isKitKat || ActivityCompat.checkSelfPermission(activity, "android.permission.RECORD_AUDIO") == 0) {
            return true;
        }
        ActivityCompat.requestPermissions(activity, PERMISSION_RECORD_AUDIO, 4);
        return false;
    }

    public static boolean verifyMultiPermissions(Activity activity) {
        if (!isKitKat) {
            return true;
        }
        ArrayList noGrantedPermission = getNoGrantedPermission(activity);
        if (noGrantedPermission == null || noGrantedPermission.size() <= 0) {
            return true;
        }
        ActivityCompat.requestPermissions(activity, (String[]) noGrantedPermission.toArray(new String[noGrantedPermission.size()]), 5);
        return false;
    }

    public static ArrayList<String> getNoGrantedPermission(Activity activity) {
        ArrayList<String> arrayList = new ArrayList<>();
        int i = 0;
        while (i < requestPermissions.length) {
            String str = requestPermissions[i];
            try {
                if (ActivityCompat.checkSelfPermission(activity, str) != 0) {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(activity, str)) {
                        arrayList.add(str);
                    } else {
                        arrayList.add(str);
                    }
                }
                i++;
            } catch (RuntimeException unused) {
                return null;
            }
        }
        return arrayList;
    }

    public static void onRequestPermissionsResult(int i, @NonNull String[] strArr, @NonNull int[] iArr) {
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        for (int i2 = 0; i2 < strArr.length; i2++) {
            String str = strArr[i2];
            if (iArr[i2] == 0) {
                arrayList.add(str);
            } else {
                arrayList2.add(str);
            }
        }
        if (callBack != null) {
            if (!arrayList.isEmpty()) {
                callBack.onPermissionsGranted(i, arrayList, arrayList2.isEmpty());
                callBack = null;
            }
            if (!arrayList2.isEmpty()) {
                callBack.onPermissionsDenied(i, arrayList2, arrayList.isEmpty());
                callBack = null;
            }
        }
    }

    public static void registerPermissionsCallBack(onRequestPermissionsResultCallbacks onrequestpermissionsresultcallbacks) {
        callBack = onrequestpermissionsresultcallbacks;
    }
}
